package com.doradance.app.appactivity.search;


import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.donkingliang.labels.LabelsView;
import com.doradance.R;
import com.doradance.account.utils.FragmentUtils;
import com.doradance.account.utils.ListDataSave;
import com.doradance.entity.ShowSearchEntity;
import com.doradance.entity.clearListEntity;
import com.pachong.android.baseuicomponent.fragment.BaseFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class SearchHomeBabyFragment extends BaseFragment {

    ArrayList<String> mArrayList = new ArrayList();
    @Bind(R.id.labels_search_baby_history)
    LabelsView mLabelsSearchBabyHistory;
    ListDataSave dataList;
    @Bind(R.id.tv_no_baby_history)
    TextView mTvNoBabyHistory;

    public SearchHomeBabyFragment() {
        // Required empty public constructor
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_search_home_baby, container, false);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        initData();
        return rootView;
    }

    private void initData() {
        mLabelsSearchBabyHistory.setSelectType(LabelsView.SelectType.NONE);
        dataList = new ListDataSave(getContext(), "searchbabayhistory");
        for (int i = 0; i < dataList.getDataList("searchbabayhistory").size(); i++) {
            mArrayList.add(dataList.getDataList("searchbabayhistory").get(i).toString());
        }
        if(dataList.getDataList("searchbabayhistory").size()==0){
            mTvNoBabyHistory.setVisibility(View.VISIBLE);
            mLabelsSearchBabyHistory.setVisibility(View.GONE);
        }
        //倒序
        Collections.reverse(mArrayList);
        //获取去掉重复的元素的集合
        ArrayList newList = getArrayList(mArrayList);
        mLabelsSearchBabyHistory.setLabels(newList, new LabelsView.LabelTextProvider<String>() {
            @Override
            public CharSequence getLabelText(TextView label, int position, String data) {
                return data;
            }
        });

        mLabelsSearchBabyHistory.setOnLabelClickListener(new LabelsView.OnLabelClickListener() {
            @Override
            public void onLabelClick(TextView label, Object data, int position) {
                mArrayList.add(label.getText().toString());
                dataList.setDataList("searchbabayhistory", mArrayList);
                SearchBabyFragment searchVideoFragment = new SearchBabyFragment(label.getText().toString());
                FragmentUtils.replaceFragment(getFragmentManager(), R.id.fl_search, searchVideoFragment);
                EventBus.getDefault().post(new ShowSearchEntity(label.getText().toString()));
            }
        });
    }


    @OnClick(R.id.iv_delete_baby_history)
    public void onViewClicked() {
        LayoutInflater factory = LayoutInflater.from(getContext());
        View inflate = factory.inflate(R.layout.no_member_dialog, null);
        TextView tvDialogTitle = (TextView) inflate.findViewById(R.id.tv_dialog_title);
        TextView tvBecomeVip = (TextView) inflate.findViewById(R.id.tv_become_vip);
        tvDialogTitle.setText(getString(R.string.clear_search));
        tvBecomeVip.setText(getString(R.string.confirm));
        final AlertDialog isMemberDialog = new AlertDialog.Builder(getContext())
                .setView(inflate).create();
        inflate.findViewById(R.id.tv_no_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isMemberDialog.cancel();
            }
        });
        inflate.findViewById(R.id.tv_become_vip).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataList.clearDataList();
                EventBus.getDefault().post(new clearListEntity(true));
                isMemberDialog.cancel();
                initData();
            }
        });
        //设置点击Dialog外部任意区域关闭Dialog
        isMemberDialog.setCanceledOnTouchOutside(true);
        isMemberDialog.show();
    }

    //去除重复元素
    public static ArrayList getArrayList(ArrayList list){
        ArrayList newList = new ArrayList();
        Iterator it = list.iterator();
        while(it.hasNext()){
            Object obj = it.next();
            if(!newList.contains(obj)){
                newList.add(obj);
            }
        }
        return newList;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
